package com.smartalarmclock.Logic;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Set;

import com.smartalarmclock.Model.ContactFactory;
import com.smartalarmclock.Model.IAutoResponse;
import com.smartalarmclock.Model.IContact;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ISingleContact;
import com.smartalarmclock.Model.ITimeFrame.DayInWeek;
import com.smartalarmclock.Model.ITimeFrame.TimeInWeek;
import com.smartalarmclock.Model.RulesItemFactory;
import com.smartalarmclock.Model.Exceptions.ConflictingSchedueledProfilesException;
import com.smartalarmclock.Model.Exceptions.PhoneContactDoesNotExistException;

import android.content.Context;
import android.util.Log;

public class AcAutoResponseService {
	
	public static class AcAutoResponseServiceStatic {
		
		private static final String TAG = "AcAutoResponseServiceStatic";
		public AcAutoResponseServiceStatic() {
			super();
		}

		// TODO: this is just a simple test implementation... Need to handle it correctly (unknown numbers
		// should sometimes also be declined which is currently not convered).
		
		private static IProfile getRunningProfile(Context i_Context) throws ConflictingSchedueledProfilesException
		{
			IProfile runningProfile = null;
			RulesItemFactory profilesFactory = new RulesItemFactory();
			Calendar c = Calendar.getInstance();
			
			DayInWeek dayInWeek = DayInWeek.FromValue(c.get(Calendar.DAY_OF_WEEK));
			Time time = new Time(c.getTimeInMillis());
			TimeInWeek currentTime = new TimeInWeek(dayInWeek, time);
			
			Log.v(TAG, "Current time: " + currentTime.toString());

			Set<IProfile> matchingProfiles = profilesFactory.GetProfilesByTimeRange(i_Context, currentTime, currentTime);
			
			if (matchingProfiles != null && matchingProfiles.size() > 1) // Only one profile should be active and enabled at a specified time.
			{
				Log.v(TAG, "For some reason, there are more than one profile active at the same time");
				String conflictMessage = ConflictingSchedueledProfilesException.CreateConflictMessage(matchingProfiles, currentTime);
				throw new ConflictingSchedueledProfilesException(conflictMessage);
			}
			
			if (matchingProfiles != null && !matchingProfiles.isEmpty())
			{
				Log.v(TAG, "running profile should be good to go");
				runningProfile = matchingProfiles.iterator().next();
			}
			
			return runningProfile;
		}
		
		public static IAutoResponse GetAutoResponseForContact(String i_PhoneNumber, Context i_Context)
		{
			Log.v(TAG, "Entered getAutoResponseForContact. Phone: " + i_PhoneNumber);
			IProfile runningProfile = null;
			IAutoResponse autoResponse = null;
			try {
				runningProfile = getRunningProfile(i_Context);
			} catch (ConflictingSchedueledProfilesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// TODO: ask merav to do this in SQL (more efficent)
			Set<IContact> contacts;
			if (runningProfile != null)
			{
				Log.v(TAG, "Running profile exists, checking info");
				contacts = runningProfile.GetContacts(i_Context);
				
				for(IContact contact : contacts)
				{					
					ArrayList<String> contactPhoneNumbers = null;
					try {
						Log.v(TAG, "Trying to get " + contact.GetName(i_Context) + " phone numbers");
						contactPhoneNumbers = ((ISingleContact) contact).GetPhoneContactNumbers(i_Context);
						Log.v(TAG, "Found "+ contactPhoneNumbers.size() + "entries");

					} catch (PhoneContactDoesNotExistException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					for (String phoneNumber : contactPhoneNumbers)
					{
						Log.v(TAG, "Current phoneNumberChecked: "+ phoneNumber);
						if (i_PhoneNumber.equals(phoneNumber))
						{
							autoResponse = runningProfile.GetMatchingAutoResponse();
							Log.v(TAG, "Contact matches Profile!");
							break;
						}
					}
				}
				
				if (autoResponse == null)
				{
					autoResponse = runningProfile.GetNonMatchingAutoResponse();
					Log.v(TAG, "Contact " + i_PhoneNumber + " doesn't match Profile!");
				}
			}
			else
			{
				Log.v(TAG, "Running profile wasn't found");
			}
			
			return autoResponse;
		}
	}

}
